Depth based blending for 3D graphics systems

ABSTRACT

Three dimensional computer graphic images are shaded by first supplying data which defines the objects to be inserted into the image, this data including depth data relating to the objects. A depth store ( 4 ) includes data for objects already inserted into the image and a depth comparison unit ( 6 ) compares the depth of an object to be inserted with the currently stored depth data for a particular pixel. The result of this is used to determine which of a number of depth operations is to be applied to the depth store ( 4 ). A blending value is selected for each pixel for combining the object with the image in dependence on the selected depth operation. A combining unit then combines the object with the image in proportions dependent upon the selecting blending value.

[0001] This invention relates to method and apparatus for shading andtexturing three dimensional computer generated graphic images and inparticular to methods and apparatus which generate so called“volumetric” effects through the use of depth based blending. Forexample a cloud, where the degree to which the cloud or any objectcontained partially or fully within is visible, is dependent on how muchof the cloud a ray cast from the eye must pass through before eitherexiting the cloud or “impacting” another object.

[0002] Software blending of volumes can be derived from the distancebetween the volume's front vertices and its back faces, or an objectthat lies between them. This is achieved by casting a ray through eachfront facing vertex and determining the distance it travels beforeimpacting either a back face of the volume or an object containedwithin. This distance can then be used as an alpha value for the vertexwhich the ray was cast through. These per vertex blend factors can thenbe interpolated across the surfaces of the volume. However, as the blendfactors are calculated per vertex any irregularities that occur inbetween them will not be taken into account. Thus, to accurately rendera scene the volume objects must contain a large number of vertices, orin the extreme case a vertex per screen pixel that is covered by thevolume. This large number of vertices coupled with the fact that a raymust be cast through and tested against a large number of other objectfaces make this technique extremely inefficient.

[0003] Most 3D graphic systems operate by using the well known techniqueof Z or depth buffering. Objects to be rendered into a scene have depthvalues associated with them which can be used to derive a depth valuefor each pixel intersected by the object. These are compared with thecurrent depth values in the depth buffer for each pixel. If an object isfound to be in front of the current stored depth a new value can beoverwritten into the depth buffer.

[0004] Many 3D graphics system also use the well known technique ofstencil buffering. This technique utilises a second parallel buffer thatwill typically contain between one and eight bits per pixel. When anobject is to be written into a scene, as well as the depth buffercomparison summarised above a stencil buffer comparison with the currentstencil buffer value is made for each pixel and a new stencil buffervalue may be written. This comparison is used to select one of a set ofstencil operations which are performed on the stencil buffer. These aredesignated as follows:

[0005] SOP1=stencil test fails,

[0006] SOP2=stencil test passes and depth test fails,

[0007] SOP3=stencil and depth tests both pass.

[0008] Thus, if the stencil test passes and the depth test fails,operation SOP2 will be assigned to the stencil buffers. Examples of theoperations which can be assigned to SOP1, SOP2 and SOP3 are as follows;

[0009] Keep=keep Stencil value,

[0010] zero=replace stencil value with zero,

[0011] Replace=replace stencil value with reference value,

[0012] INCPSAT=increment stencil with clamping at a maximum value,

[0013] DECRSAT=decrement stencil with clamping at a minimum value,

[0014] Invert=invert stencil value,

[0015] INCR=increment stencil value,

[0016] DECR=decrement stencil value.

[0017] Thus, each object in the supplied object list will have aparticular set of functions assigned to SOP1, SOP2 and SOP3. The objectstencil value and the value within the stencil buffer are then combinedusing the selected operation in dependence on the stencil and depthtests. The result is then written back to the stencil buffer. The depthis subsequently only updated if both the stencil and depth buffer testsucceed.

[0018] A preferred embodiment of the present invention provides a methodand apparatus, which are able to implement volumetric effects, such asforming clouds, efficiently. To do this it provides a set of depthbuffer operations which allow depth values to be manipulatedarithmetically. These operations allow a depth or blending value to beformed that can be representative of the distance between the front andback of the volume. After derivation, these values can be passed to atexture blending unit in which they can be used to blend othercomponents such as iterated colours, textures, or any other sourceapplicable to texture blending. The result from the texture blendingunit can then be alpha blended with the current contents of the framebuffer.

[0019] The invention is defined with more precision in the appendedclaims to which reference should now be made.

[0020] A preferred embodiment of the invention will now be described indetail, by way of example, with reference to the single figure whichshows a block diagram of an embodiment of the invention.

[0021] The figure illustrates an example of how depth based blending canbe implemented. A stencil buffer which will provide the result of thestencil test to this circuit is not shown. This, however, will bestandard stencil buffer circuitry which will be familiar to thoseskilled in the art with an extra output from the stencil test unitfeeding into the select depth operation unit 20 in the figure. Thiscircuit solves the problem faced with current hardware in whichoperations applied to the depth buffer are restricted to simple keep orreplace operations based on success or failure of the depth and stencilcomparison. This embodiment of the invention seeks to increase thenumber of different operations which can be performed on the depthbuffer thereby increasing the number of effects which can be applied.

[0022] This is achieved by the use of a select depth operation unit 20,responsive to the results of the depth comparison in unit 6 and thestencil comparison in unit B, to select one of three operations DOP1,DOP2, DOP3. These may each have one of typically 8 different operationsassigned to them for each object. The list could be longer than 8.

[0023] The operations would be selected as follows:

[0024] DOP1=stencil test fails,

[0025] DOP2=stencil test passes and depth test fails,

[0026] DOP3=stencil and depth tests both pass.

[0027] Each of these will have assigned to it one of the following depthbuffer operations:

[0028] Keep=current depth,

[0029] Zero=set depth buffer to zero,

[0030] Max=set depth buffer to its maximum value,

[0031] Replace=replace depth buffer value with object depth,

[0032] Add=add object depth to depth buffer value,

[0033] subtract=subtract depth buffer value from object depth,

[0034] Reverse subtract=subtract object depth from depth buffer value.

[0035] Note that all arithmetic operations will saturate at one andzero.

[0036] Because arithmetic operations on the depth buffer are permitted,using this system it becomes possible to construct pixel specific valuesthat reflect the distance between objects within the depth plane. Thesevalues can be used, after modification, as alpha values for a blend.

[0037] In the figure the depth compare unit 6 compares an object depthfrom the object list with the current depth stored in the depth buffer 4for a particular pixel and outputs the result to the select depthoperation unit 20. This also receives the result of the stencilcomparison unit (not shown). In response to the two input values one ofthe operations DOP1, DOP2 and DOP3 will be selected.

[0038] The selected operation will be applied in an apply depthoperation unit 22 which receives the same input as the depth comparisonunit 6. It is thus able to combine the object depth and depth buffervalue using the selected operation. This output can be written into thedepth buffer. If necessary, a write enable line (not illustrated) willbe provided to the depth buffer for this.

[0039] The range of values to be supplied by the depth buffer is definedto be in the range zero to one. However, depth based blending effectsrequire a range of depth values which correspond to the full, or somefraction of, the range of alpha blend values, again zero to one, andthis may not cover the full depth buffer range. For example, a cloudcould occupy a small range of depth values although the requiredblending affect might require a full range of alpha values. In order toovercome this problem the depth value used must be (a) offset such thatits base value lies at zero, and (b) scaled such that its maximum valuelies at either one or the required maximum. This operation comprises:

DepthAlpha=(Depth+Offset)×Scale.

[0040] The addition of the offset should be signed and should saturateat zero and one. The scale should be unsigned and should saturate atone.

[0041] These operations are performed in the offset and scale unitsnumbered 24 and 26 respectively in the figure. The depth and scalevalues are controlled by parameters associated with the object list in awell known manner.

[0042] Once the alpha value for the texture blend has been derived it isthen combined with colour or texture in a texture blend unit 28. Thisallows the depth value to be modified by the full range of textureblending operations. For example, the depth alpha value could bemodulated against a texture that represents the “density” of the volume,resulting in an alpha value which may vary per pixel in relation to bothdepth and the applied texture. The result is then used in alpha blendunit 30 to combine the new object with the current contents of the framebuffer 32 as appropriate.

[0043] The system is not limited to the use of three depth bufferoperations (DOP1, DOP2, DOP3). With both stencil and depth testcomparisons being used to generate selection bits, a total of fouroperations could be used. Alternatively if only a single bit from thedepth buffer was used, a choice between two operations could be made.

[0044] An example of use of the invention is now described for renderinga cloud into a scene to be displayed. Objects of this type, to which adepth dependent alpha are applied, will be rendered into a scene afterall other objects.

[0045] A cloud is formed from a convex mesh of polygons which is splitby the 3D graphics system into front and back facing halves in a wellknown manner. The back faces of the mesh are first rendered by assigningto the depth selection unit 20 in the figure the three alternativefunctions as follows:

[0046] DOP1=keep value of depth buffer

[0047] DOP2=keep value of depth buffer

[0048] DOP3=replace value in depth buffer

[0049] This results in well known depth buffer behavior. That is to say,the depth buffer is up-dated if the cloud polygons lie in front of thecurrent depth. These polygons are not required to up-date the framebuffer as their visibility would depend on the front half of the cloudwhich will later be rendered into the frame buffer. To achieve this anull alpha blend mode is set up such that no blending takes place. As noblending is being performed the scale and offset values need not be set.

[0050] After this, the front facing polygons of the cloud are renderedwith the following depth operations set up as inputs to select depthoperation unit 20:

[0051] DOP1=zero depth buffer value

[0052] DOP2=zero depth buffer value

[0053] DOP3=Subtract depth buffer value from current depth

[0054] This will result in either a value of zero being generated fromthe depth buffer or the distance between the front facing polygons andeither the back facing ones, or any object that lies between them beinggenerated by the is subtract operation. The value generated can then bescaled such that pixels that have a maximum value result in the requiredmaximum alpha value. Any pixel that has no distance between front andback facing polygons results in the value of zero so no offset need beapplied, i.e. it is set to zero.

[0055] The result of the scaling and offset units 24 and 26 is thenapplied to the texture blend unit 28. This can be set up such that thedepth alpha value is multiplied by the alpha of a texture that is mappedonto the front facing polygons of the cloud. The texture may contain anyimage that the application requires. In this particular example thecolour channels would represent the detail of the cloud surface and thealpha channel would represent the clouds density. As a result, an alphavalue is generated that is not only proportional to the distance betweenthe front and back of the cloud but is also proportionate to the alphachannel of the cloud texture.

[0056] The result from the texture blending unit can now be passed tothe alpha blending unit where an alpha blend with the current contentsof the frame buffer is performed in a well known manner. The blendfunction required will be a linear blend between the frame buffer valuesand those of the “modulated” text. As a result, the frame buffer imagesare obscured by the cloud texture in a manner which is proportional tothe thickness and density of the cloud. For example, where the cloud isthin or has a low density the original contents of the frame buffer willpredominate and where the cloud is thick or has a high density the cloudwill predominate. Thus, the result of the subtraction between the frontsurface of the cloud and its rear surface, or the surface or anintermediate object controls the alpha value selected.

[0057] In a further embodiment, a secondary depth buffer may be used toallow the original depth values from the frame buffer to be retained.This can be useful in situations such as when multiple intersectingvolumes are to be rendered.

[0058] It will be appreciated that DOP1 & DOP2 select the sameoperation. It is therefore apparent that the selection is being made onthe basis of passing or failing of the depth test. Therefore, asimplified embodiment would dispense with the stencil test as aselection input.

[0059] A further embodiment allows the volumes to be processed asmonolithic objects. As the volume would be presented as a “whole” it ispossible to handle multiple per pixel entries and exits to/from thevolume, as such concave volumes can be handled. Also, as the objectwould be represented as a volume, no destructive write to the depthbuffer is required.

[0060] This improvement can be implemented in any standard graphicssystem and it is hoped that it will be included in the Microsoft Direct3D Immediate Mode system for Writing 3D Graphics and will thus enablesoftware to be written to configure a standard computer to embody theinvention.

1. An apparatus for shading a three dimensional computer graphic imagecomprising a) means for supplying data defining objects to be insertedinto the image, the data including depth data relating to the object; b)means for storing depth data for objects already inserted into theimage; c) means for comparing the depth of an object to be inserted withcurrently stored depth data for a pixel, at each pixel covered by theobject to be inserted into the image; d) means for selecting a depthoperation to be applied to the means for storing depth data independence on the result of the comparison; e) means for selecting ablending value, for each pixel for combining the object with the image,in dependence on the selected depth operation; f) means for combiningthe object with the image in proportions dependent upon the selectedblending value; g) means for modifying the result of the selected depthoperation such that it is suitable for use in an alpha blendingoperation.
 2. Apparatus according to claim 1 in which the object beingshaded is of variable translucency and the blending value selected isdependent on the translucency of the object at a pixel.
 3. Apparatusaccording to claim 2 in which translucent objects are first divided intofront and rear facing portions, the rear facing portions are firstlysupplied to the depth comparison means and the depth operation selectingmeans updates the depth storing means if the rear facing surface iscloser to the eye than the currently stored surface, the front facingsurfaces are then supplied to the depth storing means and the depthoperation selection means updates the depth store with the result of asubtraction of the currently stored depth from the new depth, and theblending value is in dependence on this value.
 4. Apparatus according toclaim 3 in which the blending value selected is proportional to theresult of the subtraction.
 5. Apparatus according to claim 2 in which anobject applied to the depth operation selection means subsequentlygenerates an alpha value for use in the means for alpha blending. 6.Apparatus according to claim 1 in which an object applied to the depthoperation selection means is used to construct depth buffer values foruse during further use of depth comparison means.
 7. Apparatus forshading a three dimensional computer graphic image which includesobjects which are at least partially translucent comprising: a) meansfor supplying data defining objects to be inserted into the image; b)means for storing depth data for objects in the image; c) means forcomparing the depth of an object to be inserted into the image withcurrently stored depth data for a pixel, at each pixel covered by theobject; d) means for selecting a blending value to combine the objectwith the image in dependence on the result or the comparison and on thetranslucency of the object at that pixel; e) means for combining theobject with the image in dependence on the selected blending value.
 8. Amethod for shading in three dimensional computer graphic image whichincludes objects which are at least partially translucent comprising thesteps of: a) supplying data defining objects to be inserted into theimage; b) storing depth data or objects in the image; c) comparing thedepth of an object to be inserted into the image with currently storeddepth data for a pixel, at each pixel covered by the object; d)selecting a blending value to combine the object with the image independence on the result of the comparison and on the translucency ofthe object at that pixel; e) combining the object with the image independence on the selected blending value.